package com.zsqx.tools;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class PageDiv extends TagSupport{
	 PageBean pageBean;
     String url = "";
     String para = "";
     @Override
 	public int doStartTag() throws JspException {
    	 JspWriter out = pageContext.getOut();
    	 Integer page = pageBean.getCurrentPage();
         Integer pageCount  = pageBean.getPageCount();
         String className = "number";
         try {
        	 out.print("<div class=\"pagination\">");
			 if(page>1){
				 out.print("<a href=\""+url+"?currentPage=1&"+para+" \">首页</a>");                 
		         out.print("<a href=\""+url+"?currentPage="+(page-1)+"&"+para+" \">上一页</a>");                 
			 }else{
		         out.print("<span class='gray'>上一页 </span>");      
			 }
			 if(pageCount==1){
				 out.print("<a class=\"number current\" href=\""+url+"?currentPage="+1+"&"+para+" \">"+1+"</a>");    
			 }else if(pageCount<=5){
		         for (int i = 1; i <= pageCount; i++) {
		        	 if(page==i){
		        		 className="number current";
		        	 }else{
		        		 className="number";
		        	 }
		             out.print("<a class=\""+className+"\" href=\""+url+"?currentPage="+i+"&"+para+" \">"+i+"</a>");
		         }
			 }else{
		         out.print("<a class=\""+className+"\" href=\""+url+"?currentPage=1"+"&"+para+" \">1</a>");
		         out.print("<a class=\""+className+"\" href=\""+url+"?currentPage=2"+"&"+para+" \">2</a>");
		         if(page<=5){
	                 if(page>=2){
	                     out.print("<a class=\""+className+"\" href=\""+url+"?currentPage=3"+"&"+para+" \">3</a>");
	                 }
	                 if(page>=3){
                         out.print("<a class=\""+className+"\" href=\""+url+"?currentPage=4"+"&"+para+" \">4</a>");
	                 }
	                 if(page>=4&&pageCount>6){
                         out.print("<a class=\""+className+"\"ref=\""+url+"?currentPage=5"+"&"+para+" \">5</a>");
	                 }
	                 if(page==5&&pageCount>7){
                         out.print("<a class=\""+className+"\" href=\""+url+"?currentPage=6"+"&"+para+" \">6</a>");
	                 }
		         }
		         if(page>=6){
	                 out.print("<span style='width:30'>...</span>");
		         }
		         if(page>5&&page<pageCount-4){
	                 out.print("<a class=\""+className+"\" href=\""+url+"?currentPage="+(page-1)+"&"+para+" \">"+(page-1)+"</a>");
	                 out.print("<a class=\""+className+"\" href=\""+url+"?currentPage="+page+"&"+para+" \">"+page+"</a>");
	                 out.print("<a class=\""+className+"\" href=\""+url+"?currentPage="+(page+1)+"&"+para+" \">"+(page+1)+"</a>");
		         }
		         if(pageCount-page>=4){
	                 out.print("<span>...</span>");
		         }
		         if(page>=pageCount-4&&page>5){
	                 if(page<=pageCount-4){
	                         out.print("<a class=\""+className+"\" href=\""+url+"?currentPage="+(pageCount-5)+"&"+para+" \">"+(pageCount-5)+"</a>");
	                 }
	                 if(page<=pageCount-3){
	                         out.print("<a class=\""+className+"\" href=\""+url+"?currentPage="+(pageCount-4)+"&"+para+" \">"+(pageCount-4)+"</a>");
	                 }
	                 if(page<=pageCount-2){
	                         out.print("<a class=\""+className+"\" href=\""+url+"?currentPage="+(pageCount-3)+"&"+para+" \">"+(pageCount-3)+"</a>");
	                 }
	                 if(page<=pageCount-1){
	                         out.print("<a class=\""+className+"\" href=\""+url+"?currentPage="+(pageCount-2)+"&"+para+" \">"+(pageCount-2)+"</a>");
	                 }
		         }
		         out.print("<a class=\""+className+"\" href=\""+url+"?currentPage="+(pageCount-1)+"&"+para+" \">"+(pageCount-1)+"</a>");
		         out.print("<a class=\""+className+"\" href=\""+url+"?currentPage="+pageCount+"&"+para+" \">"+pageCount+"</a>");
			 }
			 
			 if(page<pageCount){
			     out.print("<a href=\""+url+"?currentPage="+(page+1)+"&"+para+" \">下一页</a>");         
			     out.print("<a href=\""+url+"?currentPage="+pageCount+"&"+para+" \">尾页</a>");         
			 }else{
				 out.print("<span class='gray'> 下一页 </span>");
			 }
			 out.print("</div>");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 		 return super.doStartTag();
 	}
	public PageBean getPageBean() {
		return pageBean;
	}
	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getPara() {
		return para;
	}
	public void setPara(String para) {
		this.para = para;
	}
     
     
	

}



